//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.ControlPanel.UI
{
	/// <summary>
	/// Summary description for PanelPage.
	/// </summary>
	public class PanelPage : CSConfirmationPage
	{
		public PanelPage()
		{
		}

		protected string CreateApplicationCookieName(CSContext context, string app)
		{
			return string.Format("{0}_{1}_{2}", context.SettingsID,context.User.Username,app);
		}



		protected readonly string controlPanelResourceFile = "ControlPanelResources.xml";



        protected void SetLocation()
        {
			UsersOnline.PrivateLocation();
        }

		# region User Preferences

		/// <summary>
		/// Users option to show hover help icons, Will return default values before the OnLoad event
		/// Due to dependancy on CSConect.Current.User
		/// </summary>
		public bool ShowHelpIcons
		{
			get{return Globals.SafeBool(CurrentUser.GetExtendedAttribute("CPShowHelpIcons"),true);}
		}
	
		#endregion

		private User user;
		public User CurrentUser
		{
			get
			{
				if(user == null)
					user = CSContext.Current.User;

				return user;
			}
		}
		protected override void OnInit(EventArgs e)
		{
            base.OnInit (e);

			this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
			Authorize();	
			
		}


		protected override void OnLoad(EventArgs e)
		{
		
			base.OnLoad (e);
		}


		protected virtual void Authorize()
		{
			if(!CSContext.Current.IsAuthenticated)
				Response.Redirect(Globals.GetSiteUrls().Login);
		}


	}

	public class BlogSwitchValidator : IMenuItem
	{
		public bool IsValid(HttpContext context)
		{
			ArrayList al = Weblogs.GetWeblogs(true,false,false);
			return Sections.FilterByAccessControl(al,Permission.Post).Count > 1;
		}
	}
	
	public class GallerySwitchValidator : IMenuItem
	{
		public bool IsValid(HttpContext context)
		{
			ArrayList al = CommunityServer.Galleries.Galleries.GetGalleries(true,false);
			return Sections.FilterByAccessControl(al,Permission.Post).Count > 1;
		}
	}

}
